package com.friendar.android;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.widget.*;
import android.util.Log;
import com.google.android.maps.MapView;
import java.util.*;
import java.net.*;
import java.io.*;

public class FriendarMobileService extends Activity {

	private LocationManager locMan;
	/**
	 * The provider that will be used to get the geographic information
	 */
	private LocationProvider locProv; 
	private static final String logName = "Friendar";
	private static boolean broadcast = false;
	private static boolean notify = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		TextView tv = new TextView(this);
		tv.setText("Hello, this is Ashish's first app");

		setContentView(R.layout.main);
		if (!initialize()) {
			tv.setText("Initialization failed");
		}
		Location loc = locMan.getCurrentLocation(locProv.getName());
		tv.setText(reverseGeocode(loc.getLatitude(), loc
							.getLongitude()));
		setContentView(tv);
	}
	/**
	 * This method intializes the location based services and 
	 * stores a provider for use in the future
	 * @return true if the initialization is successful
	 */
	private boolean initialize() {
		locMan = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		if (locMan != null) {
			List<LocationProvider> provs = locMan.getProviders();
			if (!provs.isEmpty()) {
				Log.d(logName, "No of providers: " + provs.size());
				for (int i = 0; i < provs.size(); i++) {
					LocationProvider prov = (LocationProvider) provs.get(i);
					Log.d(logName, "Provider " + i + ": " + prov.getName());
					Location loc = locMan.getCurrentLocation(prov.getName());
					Log.d(logName, "Longitude: " + loc.getLongitude());
					Log.d(logName, "Latitude: " + loc.getLatitude());
					Log.d(logName, reverseGeocode(loc.getLatitude(), loc
							.getLongitude()));
					locProv = provs.get(i);
				}
			}
			return true;
		}

		return false;
	}

	public void findCoordinates() {

	}

	public void syncCoordinates() {

	}

	public void handleNearbyFriends() {

	}
	/**
	 * Sets the broadcast and notification preferences. These will be
	 * triggered from an UI once that is built.
	 * @param type Indicates which preference needs to be set. Possible values are broadcast and notify
	 * @param value corresponds to type. Can be true or false
	 */
	public void handlePreferences(String type, boolean value) {
		if (type.equalsIgnoreCase("broadcast")) {
			setBroadcast(value);
		} else if (type.equalsIgnoreCase("notify")) {
			setNotify(value);
		}
	}

	private void setBroadcast(boolean val) {
		broadcast = val;
	}

	private void setNotify(boolean val) {
		notify = val;
	}
	/**
	 * Uses a reverse geo code service to do a look up of the lat and long to
	 * an address. This is displayed to the end user.
	 * @param lati latitude for th reverse lookup
	 * @param longi longitude for the reverse lookup
	 * @return currently an XML string which has the address translation
	 */
	private String reverseGeocode (double lati, double longi) {
		String line = "";
		StringBuffer sb = new StringBuffer();
		try {
			URL url = new URL("http://freereversegeo.com/api.php?lat_1=" + lati
					+ "&lng_1=" + longi);
	
			URLConnection connection = url.openConnection();
	
			connection.setDoInput(true);
	
			InputStream inStream = connection.getInputStream();
	
			BufferedReader input = new BufferedReader(new InputStreamReader(inStream));
	
			while ((line = input.readLine()) != null) {
				sb.append(line);
				Log.d(logName, line);
			}
		} catch (Exception e){
			Log.e(logName, e.getMessage());
		}
		return sb.toString();
		 
	}
	
}
